package com.app.controller.backstage;

import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.model.NoticeModel;
import com.app.service.AdminUserService;
import com.app.service.NoticeService;
import com.app.util.CommonHelp;
import com.app.util.TokenUtils;
import com.app.vo.NoticeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Controller
@RequestMapping("/behind")
public class NoticeBehindController {
    @Autowired
    private NoticeService noticeService;


    @Autowired
    private AdminUserService adminUserService;
    /**
     * 通知公告列表
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindNoticeList")
    public Result<Object> getBehindNoticeList(HttpServletRequest request, NoticeModel model){
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        Page<NoticeModel> modelPage = noticeService.getPageByModel(model);
        NoticeVo vo = new NoticeVo();
        vo.setCount((int)modelPage.getTotal());
        vo.setList(modelPage.getRecords());
        return Result.success(vo);
    }


    /**
     * 通知公告编辑
     * @return
     * @throws Exception
     */
    @PostMapping("/editNotice")
    public Result<Object> editNotice(HttpServletRequest request, @RequestBody NoticeModel model){
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if(model.getId()==null||model.getId()==0)
        {
            model.setCrtTime(CommonHelp.getNowTimestamp().toString());
            noticeService.insert(model);

        }else{
            model.setUpTime(CommonHelp.getNowTimestamp().toString());
            noticeService.update(model);
        }
        return Result.success("操作成功");
    }

    /**
     * 根据ID查询
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindNoticeById")
    public Result<Object> getNoticeById(HttpServletRequest request, Integer id){
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        NoticeModel model = noticeService.getModelBtId(id);
        return Result.success(model);
    }

    /**
     * 根据ID删除
     * @return
     * @throws Exception
     */
    @GetMapping("/deleteNotice")
    public Result<Object> deleteNotice(HttpServletRequest request, Integer id){
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
         noticeService.delete(id);
        return Result.success("操作成功");
    }


}
